package web.gkw.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import web.gkw.dao.ExpertsDAO;
import web.gkw.model.Experts;

@Component("expertsDAO")
public class ExpertsDAOImpl implements ExpertsDAO {
	
	private final static String hql_queryAll = 
			"from experts where id>:minId and id<=:maxId";
	private final static String hql_queryCount = 
			"select count(*) from experts";
	
	@Resource
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}


	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}


	@Override
	public boolean addExperts(Experts expert) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().save(expert);
		return true;
	}


	@Override
	public List<Experts> getFirstList(int page) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql_queryAll);
		int maxRow = 5 * page;
		int minRow = maxRow - 5;
//		query.setMaxResults(maxRow);
//		query.setFirstResult(minRow);
		query.setInteger("minId", minRow);
		query.setInteger("maxId", maxRow);
		List<Experts> res = query.list();
		System.out.println(res.size());
		return res;
	}


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql_queryCount);
		int res = ((Long)query.uniqueResult()).intValue();
		return res;
	}

}
